<template>
  <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
    <el-button class="buttonimg">
      <img class="showimg" :src="collapsed?imgsq:imgshow" @click="toggle(collapsed)">
    </el-button>
    <el-submenu index="2" class="submenu">
      <template slot="title">{{user.username}}</template>
      <el-menu-item @click="exit()" index="2-1"><font>退出</font></el-menu-item>
    </el-submenu>
  </el-menu>
</template>
<script>
import {PcCookie,Key} from '@/utils/cookie';
import {logout} from '@/api/auth';
export default {
  name: 'navcon',
  data() {
    return {
      collapsed: true,
      imgshow: require('../assets/img/show.png'),
      imgsq: require('../assets/img/sq.png'),
      user: {},
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    this.user = PcCookie.get(Key.userInfoKey)  ? JSON.parse(PcCookie.get(Key.userInfoKey)): null;
  },
  methods: {
    // 退出登录
    exit() {
      this.$confirm('确认退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        const accessToken = PcCookie.get(Key.accessTokenKey);
        console.log('发送退出请求')
        logout(accessToken).then(response => {
            PcCookie.remove(Key.userInfoKey);
            PcCookie.remove(Key.accessTokenKey);
            PcCookie.remove(Key.refreshTokenKey);
            window.location.href = `http://localhost:9999`;
            //window.location.href = `http://admin.r2blog.cn`;
        })
      }).catch(() => {
        // 取消删除
      });
    },
    // 切换显示
    toggle(showtype) {
      this.collapsed = !showtype
      this.$root.Bus.$emit('toggle', this.collapsed)
    }
  }
}
</script>
<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
}
.submenu {
  float: right;
}
.buttonimg {
  height: 60px;
  background-color: transparent;
  border: none;
}
.showimg {
  width: 26px;
  height: 26px;
  position: absolute;
  top: 17px;
  left: 17px;
}
.showimg:active {
  border: none;
}
</style>
